package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserBookmarkRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserBookmarkMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserBookmark;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserBookmarkService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户收藏夹业务层处理
 *
 * @author mars
 * @date 2024-03-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserBookmarkServiceImpl implements ISysUserBookmarkService {

    private final SysUserBookmarkMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserBookmark add(SysUserBookmarkRequest request) {
        SysUserBookmark entity = SysUserBookmark.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setUserId(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserBookmarkRequest request) {
        SysUserBookmark entity = SysUserBookmark.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserBookmark getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserBookmark> pageList(SysUserBookmarkRequest request) {
        Page<SysUserBookmark> page = new Page<>(request.getPageNo(), request.getPageSize());
        Long userId = ContextUserInfoThreadHolder.get().getId();
        request.setUserId(userId);
        LambdaQueryWrapper<SysUserBookmark> query = this.buildWrapper(request);
        IPage<SysUserBookmark> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserBookmark> list(SysUserBookmarkRequest request) {
        LambdaQueryWrapper<SysUserBookmark> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysUserBookmark> buildWrapper(SysUserBookmarkRequest param) {
        LambdaQueryWrapper<SysUserBookmark> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysUserBookmark::getName, param.getName());
        }
        if (param.getUserId() != null) {
            query.like(SysUserBookmark::getUserId, param.getUserId());
        }
        return query;
    }

}
